package com.example.userdemo.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.example.userdemo.entity.User;
import com.example.userdemo.mapper.UserMapper;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.TimeZone;

@Service
public class UserService {
    @Resource
    private UserMapper userMapper;

    public int addUser(String name, int sex, String description){
        User user = new User();
        user.setName(name);
        user.setSex(sex);
        user.setCreateTime(new Date());
        user.setDescription(description);
        return userMapper.insert(user);
    }

    public int deleteUserById(int id){
        int delete = userMapper.deleteById(id);
        return delete;
    }

    public int deleteUserByName(String name){//可能批量删除
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("name", name);
        int delete = userMapper.delete(queryWrapper);
        return delete;
    }

    public int deleteUserByTime(String time) throws ParseException {//删除time之前的数据
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date dateTime = format.parse(time);
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        queryWrapper.le("create_time", dateTime);
        int delete = userMapper.delete(queryWrapper);
        return delete;
    }

    public User updateUserById(int id, String name, String description){
        User user = userMapper.selectById(id);
        if (user == null){
            return new User(id, name, 0, new Date(), description);
        }
        UpdateWrapper<User> updateWrapper = new UpdateWrapper<>();
        updateWrapper.set("name",name).set("description",description).eq("id",id);
        int update = userMapper.update(null, updateWrapper);
        return userMapper.selectById(id);
    }

    public User selectUserById(int id){
        User user = userMapper.selectById(id);
        return user;
    }

    public List<User> selectUserByName(String lastName){//根据姓氏查询
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        queryWrapper.likeRight("name", lastName);
        List<User> users = userMapper.selectList(queryWrapper);
        return users;
    }

    public List<User> selectUserByWord(String word){//按关键字
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        queryWrapper.like("name", word);
        List<User> users = userMapper.selectList(queryWrapper);
        return users;
    }

    public List<User> selectUserByTime(String begin,String end) throws ParseException {
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date beginDate = format.parse(begin);
        SimpleDateFormat format1 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date endDate = format1.parse(end);
        queryWrapper.between("create_time", beginDate, endDate);
        List<User> users = userMapper.selectList(queryWrapper);
        return users;
    }

}
